<template>
  <div class="app-container">
    <!-- 发起催单页面 -->
    <workOrder v-model="form" />
    <customerInformation v-model="customerList" :type="$route.query.type" />
    <collectionInformationCS v-model="form" ref="collectionInformationCS" state="write" />
    <div style="text-align: right; box-sizing: border-box; padding-right: 20px; margin-top: 20px; background-color: #fff; width: 100%; height: 60px; line-height: 60px; border-radius: 8px">
      <el-button @click="cancel">取消</el-button>
      <el-button @click="sure" type="primary">确定</el-button>
    </div>
  </div>
</template>

<script>
import workOrder from '../components/workOrder'
import customerInformation from '../components/customerInformation'
import collectionInformationCS from '../components/collectionInformationCS'
import { getInitiateACollectionDetail, saveCollectionWarning } from '@/api/collectionModule'
export default {
  // 发起催单
  name: 'InitiateACollection',
  components: {
    workOrder,
    customerInformation,
    collectionInformationCS,
  },
  data() {
    return {
      customerList: [],
      form: { processorNames: '' },
    }
  },
  created() {
    this.getInitiateACollectionDetail()
  },
  methods: {
    // 发起催收-信息
    async getInitiateACollectionDetail() {
      try {
        let res = await getInitiateACollectionDetail(this.$route.query.ids)
        this.customerList = res.data
        let workOrderScheme = res.data[0]?.selectiveWorkOrder?.length > 0 ? 1 : 2
        this.form = {
          ...res.data[0],
          workOrderScheme,
        }
      } catch (error) {}
    },
    cancel() {
      this.$tab.closeOpenPage(this.$route)
      this._toPage({
        name: this.$route.query.beFrom,
      })
    },
    sure() {
      let isValidate = this.$refs.collectionInformationCS.validate()
      let radio = this.$refs.collectionInformationCS.radio
      if (isValidate) {
        if (this.form.workOrderScheme == 1 && !radio) {
          return this.$message.warning('请先勾选一条数据')
        }
        if (this.form.processorNames) {
          this.form.processorIds = this.form?.processorIds?.toString()
          const loading = this.$loading({
            lock: true,
            text: '保存中',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)',
          })
          // 通过走接口
          let data = {
            ...this.form,
            ids: this.$route.query.ids,
            workOrderId: this.$refs.collectionInformationCS?.radio,
          }
          saveCollectionWarning(data)
            .then((res) => {
              if (res.code == '200') {
                loading.close()
                this.$message.success('保存成功')
                this.cancel()
              }
            })
            .catch((err) => {
              loading.close()
            })
        } else {
          this.$message.error('处理人不能为空')
        }
      } else {
        this.$nextTick(() => {
          const el = document.querySelectorAll('.el-form-item__error')[0]
          el.scrollIntoView({ block: 'center', behavior: 'smooth' })
        })
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.app-container {
  overflow: auto !important;
}
</style>
